import { Injectable } from '@angular/core'
import { HttpEvent,HttpInterceptor,HttpHandler,HttpRequest,HttpResponse} from '@angular/common/http'
import {tap} from "rxjs/operators";
import {Observable} from "rxjs";
import {HttpClient} from "@angular/common/http";
import {error} from "selenium-webdriver";

@Injectable()
export class RefreshInterceptor implements HttpInterceptor{
  constructor(private http:HttpClient){
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      tap(
        ()=>{},
        error=>{
          console.log(error);
          if(error.status==500&&error.error.message==='refresh fail'){
            window.location.href='http://auth.test.com:8085/oauth/authorize?'+
              'client_id=admin&'  //哪个客户端应用跳转过来的
              +'redirect_uri=http://admin.test.com:8080/oauth/callback&'   //回调地址，认证服务器发送回来的授权码接收地址
              +'response_type=code&'
              +'state=abc';
//            this.logout();
          }
        }
      )
    )
  }

  logout(){
    this.http.post('logout',{}).subscribe(()=>{
      window.location.href='http://auth.imooc.com:9090/logout?redirect_uri=http://admin.test.com:8080'
    })
  }
}
